package com.yzt.redpack;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.yzt.redpack.adapter.GameTransHostAdapter;

import com.yzt.zhuju.mvp.CommonPresenter;
import com.yzt.zhuju.mvp.MVPActivity;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.game.GameDetailBean;
import com.yzt.zhuju.network.entity.game.GameUserListBean;
import com.yzt.zhuju.network.entity.message.GroupBaseInfo;
import com.yzt.zhuju.network.entity.message.GroupUserInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：bobwang
 * 创建时间：2023-10-12 00:14:20
 * 描述：游戏转移主持人
 */
public class GameTransHostActivity extends MVPActivity<CommonPresenter> {

    private GameTransHostAdapter adapter;

    private int extraType = 1;

    private String activityId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_game_trans_host;
    }

    @Override
    public void installViews() {
        extraType = getIntent().getIntExtra("extra_type", 1);
        activityId = getIntent().getStringExtra("extra_id");
        if (extraType == 1) {
            mTitleBar.setTitle("转移给谁");
        } else if (extraType == 2) {
            mTitleBar.setTitle("指定领取人");
        } else if (extraType == 3) {
            mTitleBar.setTitle("指定领取人");
        }

        mTitleBar.setRightContent("确定");
        mTitleBar.setRightBtnListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uId = "";
                String avatar = "";
                String name = "";

                for (GameUserListBean bean : adapter.getData()) {
                    if (bean.isChecked()) {
                        uId = bean.getUserId();
                        avatar = bean.getAvatar();
                        name = bean.getNickName();
                    }
                }
                if (uId.equals("")) {
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("extra_data", uId);
                intent.putExtra("extra_avatar", avatar);
                intent.putExtra("extra_user_name", name);

                setResult(RESULT_OK, intent);
                finish();
            }
        });
        RecyclerView recycle = findViewById(R.id.blacklist_rv);
        recycle.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new GameTransHostAdapter();
        recycle.setAdapter(adapter);

        adapter.setOnItemClickListener((adapter1, view, position) -> {
            for (GameUserListBean bean : adapter.getData()) {
                bean.setChecked(false);
            }
            adapter.getItem(position).setChecked(true);
            adapter.notifyDataSetChanged();
        });

        getHttpData();
    }

    /**
     * 获取数据
     */
    public void getHttpData() {
        JSONObject json = new JSONObject();

        if (extraType == 3) {
            BizLayer.getInstance().getMessageModule().groupChatUserList(activityId, false, new AresSubscriber<GroupBaseInfo>() {
                @Override
                protected void onSuccess(GroupBaseInfo bean) {
                    List<GroupUserInfo> data = bean.getMemberList();
                    List<GameUserListBean> list = new ArrayList<>();
                    for (GroupUserInfo user : data) {
                        GameUserListBean userBean = new GameUserListBean();
                        userBean.setAvatar(user.getAvatar());
                        userBean.setGender(user.getGender());
                        userBean.setNickName(user.getMemberAlias());
                        userBean.setUserId(user.getUserId());
                        list.add(userBean);
                    }
                    adapter.setList(list);
                }

                @Override
                protected void onError(int code, String info) {
                    super.onError(code, info);
                }

                @Override
                protected void onFinallyEnd() {
                    super.onFinallyEnd();
                }
            });
        } else {
            json.put("activityId", activityId);
            json.put("oneself", false);
            BizLayer.getInstance().getDynamicModule().getGamePlayUser(json, new AresSubscriber<GameDetailBean>() {
                @Override
                protected void onSuccess(GameDetailBean bean) {
                    adapter.setList(bean.getList());
                }
            });
        }
    }
}